﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using rFDevLib;

namespace TGMResize {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    struct TireSpecs {
        public double TreadWidth, SidewallHeight, RimWidth, RimRadius;
    }

    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        rFDataFile TireIn;
        rFDataFile TireOut;

        TireSpecs Original;

        bool ControlsEnabled = false;

        private void BtnOpen_Click( object sender, RoutedEventArgs e ) {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = string.Empty;
            dlg.DefaultExt = ".ini";
            dlg.Filter = "rFactor 2 tire (.tgm)|*.tgm";

            Nullable<bool> result = dlg.ShowDialog();

            if ( result != true ) return;

            try {
                TireIn = new rFDataFile( dlg.FileName );

                TireOut = new rFDataFile();

                foreach ( rFParam a in TireIn.m_params ) {
                    TireOut.m_params.Add( a );
                }

                ControlsEnabled = true;

                InTreadWidth.IsEnabled =
                    InRimWidth.IsEnabled =
                    InRimRadius.IsEnabled = true;
                InHeightOrAspect_Click( null, null );


                double MinX = double.PositiveInfinity, MinY = double.PositiveInfinity;
                double MaxX = double.NegativeInfinity, MaxY = double.NegativeInfinity;
                int MaxNode = -1;

                foreach ( rFParam a in TireIn.m_params ) {
                    if ( a.section == "Node" && a.name == "Geometry" ) {
                        MinX = Math.Min( MinX, a.GetDouble( 0 ) );
                        MaxX = Math.Max( MaxX, a.GetDouble( 0 ) );
                        MinY = Math.Min( MinY, a.GetDouble( 1 ) );
                        MaxY = Math.Max( MaxY, a.GetDouble( 1 ) );
                        MaxNode = Math.Max( MaxNode, a.sectionNum );
                    }
                }

                double Rim1 = 0.0, Rim2 = 0.0;
                double RimY = 0.0;

                foreach ( rFParam a in TireIn.m_params ) {
                    if ( a.section == "Node" && a.name == "Geometry" ) {
                        if ( a.sectionNum == 0 ) {
                            Rim1 = a.GetDouble( 0 );
                            RimY = a.GetDouble( 1 );
                        }
                        if ( a.sectionNum == MaxNode )
                            Rim2 = a.GetDouble( 0 );
                    }
                }

                Original.RimRadius = -RimY;
                Original.RimWidth = Rim1 - Rim2;
                Original.TreadWidth = MaxX - MinX;
                Original.SidewallHeight = RimY - MinY;

                InRimRadius.Value = Original.RimRadius;
                InTreadWidth.Value = Original.TreadWidth;
                InAspectRatio.Value = Original.SidewallHeight / Original.TreadWidth;
                InSidewallHeight.Value = Original.SidewallHeight;

            } catch ( Exception exc ) {
                MessageBox.Show( "Uh oh, there's been a problem! Please send a screenshot of this dialog to the developer.\n\n" + exc.ToString(), "Unable to open this tire", MessageBoxButton.OK, MessageBoxImage.Warning );
            }
        }

        private void InHeightOrAspect_Click( object sender, RoutedEventArgs e ) {
            if ( !ControlsEnabled ) return;
            if ( InHeightOrAspect.IsChecked == true ) {
                InSidewallHeight.IsEnabled = false;
                InAspectRatio.IsEnabled = true;
            } else {
                InSidewallHeight.IsEnabled = true;
                InAspectRatio.IsEnabled = false;
            }
        }

        private void In_OnChange( object sender, EventArgs e ) {
            if ( InHeightOrAspect.IsChecked == true ) {
                InSidewallHeight.Value = InTreadWidth.Value * InAspectRatio.Value;
            } else {
                InAspectRatio.Value = InSidewallHeight.Value / InTreadWidth.Value;
            }
            return;
        }

        private void BtnSave_Click( object sender, RoutedEventArgs e ) {
            double XScale = InTreadWidth.Value / Original.TreadWidth;
            double YScale = InSidewallHeight.Value / Original.SidewallHeight;

            if(InHeightOrAspect.IsChecked == true)
                YScale = (InAspectRatio.Value * InTreadWidth.Value) / Original.SidewallHeight;

            foreach ( rFParam a in TireOut.m_params ) {
                if ( a.section == "Node" && a.name == "Geometry" ) {
                    a.values[0] = (a.GetDouble( 0 ) * XScale).ToString();
                    double y = a.GetDouble( 1 ) + Original.RimRadius;
                    y = (y * YScale) - InRimRadius.Value;
                    a.values[1] = y.ToString();
                }
            }

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "NewTire.tgm";
            dlg.DefaultExt = ".tgm";
            dlg.Filter = "rFactor 2 tires (.tgm)|*.tgm";

            Nullable<bool> result = dlg.ShowDialog();

            if ( result != true ) return;

            System.IO.StreamWriter w = new System.IO.StreamWriter( dlg.FileName );
            w.WriteLine(TireOut.ToString());
            w.Close();
        }
    }
}
